package com.zbj.hystrix.consumer;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * HystrixConsumerController
 *
 * @author weigang
 * @create 2019-08-09
 **/
@RestController
@RequestMapping("/hystrix/consumer")
public class HystrixConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     *  fallbackMethod 指向同类中的方法名
     *  当方法hello() 调用失败 就会自动转而执行 fallbackMethod对应字符串的方法名
     * @param world
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloFallbackMethod")
    @GetMapping("/hello")
    public String hello(String world) {
        // 此处也可以使用feign http://localhost:8094/hystrix.stream(hystrix控制台) 都能获取到数据
        return restTemplate.getForObject("http://hystrix-provider/hystrix/provider/hello?world=" + world, String.class);
    }

    public String helloFallbackMethod(String world) {
        return "result error";
    }
}